package com.blog.blogvue.controller;

import com.blog.admin.mapper.BlogArticleMapper;
import com.blog.admin.mapper.BlogClassifyMapper;
import com.blog.admin.mapper.BlogCommentMapper;
import com.blog.admin.mapper.BlogMessageMapper;
import com.blog.admin.mapper.apiMapper.ArticleMapper;
import com.blog.admin.service.BlogArticleService;
import com.blog.admin.service.BlogMessageService;
import com.blog.admin.vo.*;
import com.blog.common.ApiIntercept;
import com.blog.util.RedisUtil;
import com.blog.util.Result;
import com.blog.vo.ConfigVO;
import com.blog.vo.PageVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author tangqj
 * @Description 文章api接口
 * @create 2020/3/5 11:03
 */
@Controller
@Slf4j
@RequestMapping("/api/ArticleApiController")
public class ArticleApiController {
    @Autowired
    BlogArticleService blogArticleService;
    @Autowired
    RedisUtil redisUtil;
    @Autowired
    BlogClassifyMapper blogClassifyMapper;
    @Autowired
    BlogArticleMapper blogArticleMapper;
    @Autowired
    BlogCommentMapper blogCommentMapper;
    @Autowired
    BlogMessageMapper blogMessageMapper;
    @Autowired
    BlogMessageService blogMessageService;
    @Autowired
    ArticleMapper articleMapper;

    /**
     * 获取文章接口
     *
     * @param response      相应头
     * @param page          分页
     * @param blogArticleVO
     * @return
     */
    @GetMapping("getBlogArticle")
    public void getBlogArticle(HttpServletResponse response, PageVO page, BlogArticleVO blogArticleVO) throws UnknownHostException {
        PageHelper.startPage(page.getPage(), page.getLimit());
        List<BlogArticleVO> list = articleMapper.getArticle(blogArticleVO.getId(), blogArticleVO.getRealname(), blogArticleVO.getTitle(), blogArticleVO.getCatalogs());
        PageInfo<BlogArticleVO> result = new PageInfo<BlogArticleVO>(list);
        List<BlogArticleVO> blogArticleVOS = result.getList();
        List<BlogClassifyVO> blogClassifyVOS = blogClassifyMapper.getBlogClassify(new BlogClassifyVO());
        //分类名称 将id替换
        for (BlogArticleVO blogArticle : blogArticleVOS) {
            blogArticle.setThumbimg("http://" + ConfigVO.ip + ":" + ConfigVO.port + "/" + blogArticle.getThumbimg());
            // 暂存分类
            String catalogs = blogArticle.getCatalogs();
            blogArticle.setCatalogs("");
            for (BlogClassifyVO blogClassifyVO : blogClassifyVOS) {
                if (catalogs.indexOf(blogClassifyVO.getId()) > -1) {
                    //分类名称拼装
                    blogArticle.setCatalogs(blogArticle.getCatalogs() + "," + blogClassifyVO.getClassifyName());
                }
            }
            //有分类的时候进行分割关键词
            if (blogArticle.getCatalogs().length() > 1) {
                blogArticle.setCatalogs(blogArticle.getCatalogs().substring(1, blogArticle.getCatalogs().length()));
            }
        }
        Map<String, Object> data = new HashMap<>();
        data.put("count", result.getTotal());
        Result.print(response, Result.responeAjaxMap(true, "", blogArticleVOS, data));
    }

    /**
     * 根据文章id获取文章
     *
     * @param response
     * @param blogArticleVO
     */
    @GetMapping("getBlogArticleById")
    public void getBlogArticleById(HttpServletResponse response, BlogArticleVO blogArticleVO) {
        List<BlogArticleVO> blogArticleVOS = blogArticleMapper.getBlogArticle(blogArticleVO);
        if (blogArticleVOS.size() > 0) {
            // 文章浏览数+1
            blogArticleVO = blogArticleVOS.get(0);
            int count = 1;
            count = blogArticleVO.getViewCount() + 1;
            articleMapper.updateArticleViewCount(count, blogArticleVO.getId());
            //获取文章评论封装数据(一次性全部)
            BlogCommentVO param = new BlogCommentVO();
            param.setPkArticle(blogArticleVO.getId());
            //获取评论
            List<BlogCommentVO> blogCommentVOS = blogCommentMapper.getBlogComment(param);
            // 获取回复
            for (BlogCommentVO blogCommentVO : blogCommentVOS) {
                List<BlogCommentVO> reply_blogCommentVOS = blogCommentMapper.getBlogComment(blogCommentVO);
                blogCommentVO.setReply(reply_blogCommentVOS);
            }
            blogArticleVO.setComment(blogCommentVOS);
            //获取文章评论数
            blogArticleVO.setCommentCount(articleMapper.getArticleCommentCount(blogArticleVO.getId()));
            Result.print(response, Result.responeAjaxMap(true, "", blogArticleVO, null));
        } else {
            Result.print(response, Result.responeAjaxMap(false, "找不到该文章的信息", blogArticleVOS, null));
        }
    }

    /**
     * 分页获取文章评论及回复
     *
     * @param pageVO
     * @param response
     * @param blogComment
     */
    @GetMapping("getBlogArticleCommentById")
    public void getBlogArticleCommentById(PageVO pageVO, HttpServletResponse response, BlogCommentVO blogComment) {
        //分页获取评论
        PageHelper.startPage(pageVO.getPage(), pageVO.getLimit());
        List<BlogCommentVO> blogCommentVOS = blogCommentMapper.getBlogComment(blogComment);
        PageInfo<BlogCommentVO> pageRest = new PageInfo<BlogCommentVO>(blogCommentVOS);
        // 获取回复
        for (BlogCommentVO blogCommentVO : pageRest.getList()) {
            List<BlogCommentVO> reply_blogCommentVOS = blogCommentMapper.getBlogCommentAll(blogCommentVO);
            blogCommentVO.setReply(reply_blogCommentVOS);
        }
        Result.print(response, Result.responeAjaxMap(true, "", pageRest, null));
    }

    /**
     * 分页获取文章评论及回复
     *
     * @param pageVO
     * @param response
     * @param blogComment
     */
    @GetMapping("getBlogArticleCommentReplyById")
    public void getBlogArticleCommentReplyById(PageVO pageVO, HttpServletResponse response, BlogCommentVO blogComment) {
        //分页获取评论
        PageHelper.startPage(pageVO.getPage(), pageVO.getLimit());
        List<BlogCommentVO> reply_blogCommentVOS = blogCommentMapper.getBlogCommentAll(blogComment);
        PageInfo<BlogCommentVO> pageRest = new PageInfo<BlogCommentVO>(reply_blogCommentVOS);
        Result.print(response, Result.responeAjaxMap(true, "", pageRest, null));
    }


    /**
     * 评论插入
     *
     * @param response
     * @param blogCommentVO
     */
    @PostMapping("insertBlogArticleComment")
    @ApiIntercept
    public void insertBlogArticleComment(@RequestBody Map<String, Object> param, HttpServletResponse response, BlogCommentVO blogCommentVO) throws Exception {
        blogCommentVO = Result.mapToBean(param, BlogCommentVO.class);
        // 对应数据转换
        int result = blogCommentMapper.insertBlogComment(blogCommentVO);
        if (result > 0) {
            Result.print(response, Result.responeAjaxMap(true, "评论成功", blogCommentVO, null));

        } else {
            Result.print(response, Result.responeAjaxMap(false, "", null, null));
        }
    }

    /**
     * 文章插入
     *
     * @param response
     * @param blogArticleVO
     */
    @PostMapping("insertBlogArticle")
    @ApiIntercept
    public void insertBlogArticle(@RequestBody Map<String, Object> param, HttpServletResponse response, BlogArticleVO blogArticleVO) throws Exception {
        blogArticleVO = Result.mapToBean(param, BlogArticleVO.class);
        // 对应数据转换
        boolean result = blogArticleService.insertBlogArticle(blogArticleVO);
        Result.print(response, Result.responeAjaxMap(result, null, null, null));
    }


    /**
     * 获取热门文章
     *
     * @param response
     */
    @GetMapping("getHotArticle")
    public void getHotArticle(HttpServletResponse response) {
        List<Map<String, Object>> blogArticleVOS = articleMapper.getHotArticle();
        Result.print(response, Result.responeAjaxMap(true, "", blogArticleVOS, null));
    }

    /**
     * 获取最新文章
     *
     * @param response
     */
    @GetMapping("getNewArticle")
    public void getNewArticle(HttpServletResponse response) {
        List<Map<String, Object>> blogArticleVOS = articleMapper.getNewArticle();
        Result.print(response, Result.responeAjaxMap(true, "", blogArticleVOS, null));
    }

    /**
     * 根据标签获取文章
     *
     * @param response
     */
    @GetMapping("getArticleByClassName")
    public void getArticleByClassName(PageVO page,String className, HttpServletResponse response) {
//        PageVO.getPage(page);
        PageHelper.startPage(page.getPage(), page.getLimit());
        Map<String,Object> result = new HashMap<>();
        List<BlogArticleVO> blogArticleVOS1 = articleMapper.getArticleByClassName(className);
        PageInfo<BlogArticleVO> result_data = new PageInfo<BlogArticleVO>(blogArticleVOS1);
        List<BlogArticleVO> blogArticleVOS = result_data.getList();
        List<BlogClassifyVO> blogClassifyVOS = blogClassifyMapper.getBlogClassify(new BlogClassifyVO());
        //分类名称 将id替换
        for (BlogArticleVO blogArticle : blogArticleVOS) {
            blogArticle.setThumbimg(" http://" + ConfigVO.ip + ":" + ConfigVO.port + "/" + blogArticle.getThumbimg());
            // 暂存分类
            String catalogs = blogArticle.getCatalogs();
            blogArticle.setCatalogs("");
            for (BlogClassifyVO blogClassifyVO : blogClassifyVOS) {
                if (catalogs.indexOf(blogClassifyVO.getId()) > -1) {
                    //分类名称拼装
                    blogArticle.setCatalogs(blogArticle.getCatalogs() + "," + blogClassifyVO.getClassifyName());
                }
            }
            //有分类的时候进行分割关键词
            if (blogArticle.getCatalogs().length() > 1) {
                blogArticle.setCatalogs(blogArticle.getCatalogs().substring(1, blogArticle.getCatalogs().length()));
            }
        }
        result.put("count",result_data.getTotal());
        Result.print(response, Result.responeAjaxMap(true, "", blogArticleVOS, result));
    }

    /**
     * 获取标签
     *
     * @param response
     */
    @GetMapping("getClassify")
    public void getClassify(HttpServletResponse response) {
        List<BlogClassifyVO> blogClassifyVOS = articleMapper.getClassify();
        Result.print(response, Result.responeAjaxMap(true, "", blogClassifyVOS, null));
    }

}
